package com.example.demo.model.goods.goods.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Results.BasePage;
import com.example.demo.Results.ResultClass;
import com.example.demo.model.goods.goods.entity.ShopGoods;
import com.example.demo.model.goods.goods.entity.ShopGoodsType;
import com.example.demo.model.goods.goods.entity.reg.ShopGoodsListResp;
import com.example.demo.model.goods.goods.mapper.ShopGoodsDao;
import com.example.demo.model.goods.goods.mapper.ShopGoodsTypeDao;
import com.example.demo.model.goods.goods.service.ShopGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author STAHYW
 * @since 2021-10-02
 */
@Service
public class ShopGoodsServiceImpl extends ServiceImpl<ShopGoodsDao, ShopGoods> implements ShopGoodsService {

    @Value("${my.upload.dir}")
    String filePath;
    @Value("${my.upload.file.dir}")
    String fileGetPath;

    @Autowired
    ShopGoodsDao shopGoodsDao;

    @Autowired
    ShopGoodsTypeDao shopGoodsTypeDao;

    @Override
    public ModelAndView goodsList(Integer pageNo, Integer pageSize, String shopGoods) {

        ModelAndView modelAndView = new ModelAndView();

        Page page = BasePage.getPage(pageNo, pageSize);

        IPage<ShopGoodsListResp> iPage = shopGoodsDao.selectGoodsListPage(page, shopGoods);


        modelAndView.addObject("iPage", iPage);

        modelAndView.setViewName("/goods/goodsList");

        return modelAndView;
        
       
    }

    @Override
    public ResultClass goodsDel(Integer id) {

        if(id !=null){
            shopGoodsDao.deleteById(id);
        }else {
            return ResultClass.fail("操作失败！");
        }
        return ResultClass.ok("操作成功！");
    }

    @Override
    public ModelAndView goodsTypeEdit(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        List<ShopGoodsType> shopGoodsTypeList = shopGoodsTypeDao.selectList(null);
        modelAndView.addObject("shopGoodsTypeList", shopGoodsTypeList);
        if (id != null) {
            ShopGoods shopGoods = shopGoodsDao.selectById(id);
            modelAndView.addObject("shopGoods", shopGoods);
        }
        modelAndView.setViewName("/goods/goodsEdit");
        return modelAndView;
    }

    @Override
    public ResultClass goodsTypeEditDo(ShopGoods shopGoods, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            UUID uuid = UUID.randomUUID();
            String fileName = uuid + ".jpg";
            File dest = new File(filePath + fileName);
            try {
                file.transferTo(dest);
                shopGoods.setShopGoodsImg(fileGetPath + fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (shopGoods.getId()!=null){
            shopGoodsDao.updateById(shopGoods);
        }else {
            shopGoodsDao.insert(shopGoods);
        }
        return ResultClass.ok("操作成功！");
    }

}
